package com.palace.saw.module.app.controller.admin.configdata;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.palace.saw.framework.common.pojo.PageParam;
import com.palace.saw.framework.common.pojo.PageResult;
import com.palace.saw.framework.common.pojo.CommonResult;
import com.palace.saw.framework.common.util.object.BeanUtils;
import static com.palace.saw.framework.common.pojo.CommonResult.success;

import com.palace.saw.framework.excel.core.util.ExcelUtils;

import com.palace.saw.framework.apilog.core.annotation.ApiAccessLog;
import static com.palace.saw.framework.apilog.core.enums.OperateTypeEnum.*;

import com.palace.saw.module.app.controller.admin.configdata.vo.*;
import com.palace.saw.module.app.dal.dataobject.configdata.ConfigDataDO;
import com.palace.saw.module.app.service.configdata.ConfigDataService;

@Tag(name = "管理后台 - 配置项数据")
@RestController
@RequestMapping("/app/config-data")
@Validated
public class ConfigDataController {

    @Resource
    private ConfigDataService configDataService;

    @PostMapping("/create")
    @Operation(summary = "创建配置项数据")
    @PreAuthorize("@ss.hasPermission('app:config-data:create')")
    public CommonResult<Long> createConfigData(@Valid @RequestBody ConfigDataSaveReqVO createReqVO) {
        return success(configDataService.createConfigData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配置项数据")
    @PreAuthorize("@ss.hasPermission('app:config-data:update')")
    public CommonResult<Boolean> updateConfigData(@Valid @RequestBody ConfigDataSaveReqVO updateReqVO) {
        configDataService.updateConfigData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配置项数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('app:config-data:delete')")
    public CommonResult<Boolean> deleteConfigData(@RequestParam("id") Long id) {
        configDataService.deleteConfigData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配置项数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('app:config-data:query')")
    public CommonResult<ConfigDataRespVO> getConfigData(@RequestParam("id") Long id) {
        ConfigDataDO configData = configDataService.getConfigData(id);
        return success(BeanUtils.toBean(configData, ConfigDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配置项数据分页")
    @PreAuthorize("@ss.hasPermission('app:config-data:query')")
    public CommonResult<PageResult<ConfigDataRespVO>> getConfigDataPage(@Valid ConfigDataPageReqVO pageReqVO) {
        PageResult<ConfigDataDO> pageResult = configDataService.getConfigDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配置项数据 Excel")
    @PreAuthorize("@ss.hasPermission('app:config-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigDataExcel(@Valid ConfigDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigDataDO> list = configDataService.getConfigDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配置项数据.xls", "数据", ConfigDataRespVO.class,
                        BeanUtils.toBean(list, ConfigDataRespVO.class));
    }

}